﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class Admin_Views_module_category : System.Web.UI.UserControl
{
    CategoryBus cateBus = new CategoryBus();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadCategory();
            if (!string.IsNullOrEmpty(Request.QueryString["warning"]))
            {
                warning.Visible = true;
                warning.TagName = Request.QueryString["warning"];
            }

            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                LoadCategoryByID(Convert.ToInt32(Request.QueryString["ID"].ToString()));
            }
        }
    }

    public void LoadCategory()
    {
        string id = "0";
        DataTable dt = cateBus.GetAllCategory();
        parent_id.DataSource = dt;
        ListItem lii = new ListItem();
        lii.Text = "--Danh mục gốc--";
        lii.Value = id;
        parent_id.Items.Add(lii);

        if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
        {
            id = Request.QueryString["ID"].ToString();
        }

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string name = "";
            if (Convert.ToInt32(dt.Rows[i]["parent_id"].ToString()) > 0)
            {
                name += GetChildCategory(Convert.ToInt32(dt.Rows[i]["parent_id"].ToString()), dt.Rows[i]["category_name"].ToString());
            }
            if (!id.Equals(dt.Rows[i]["category_id"].ToString()))
            {
                if (string.IsNullOrEmpty(name))
                    name += dt.Rows[i]["category_name"].ToString();
                ListItem li = new ListItem();
                li.Text = name;
                li.Value = dt.Rows[i]["category_id"].ToString();
                parent_id.Items.Add(li);
            }
        }
    }

    public string GetChildCategory(int category_id, string name)
    {
        string parentName = "";
        DataTable dt = cateBus.GetCategoryByID(category_id);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (Convert.ToInt32(dt.Rows[i]["parent_id"].ToString()) > 0)
                parentName += GetChildCategory(Convert.ToInt32(dt.Rows[i]["parent_id"].ToString()), dt.Rows[i]["category_name"].ToString()) + " > " + name;
            else
                parentName += dt.Rows[i]["category_name"] + " > " + name;
        }
        return parentName;
    }

    public void LoadCategoryByID(int category_id)
    {
        DataTable dt = cateBus.GetCategoryByID(category_id);
        category_name.Text = dt.Rows[0]["category_name"].ToString();
        category_description.Text = dt.Rows[0]["category_des"].ToString();
        category_image.ImageUrl = "../../../images/" + dt.Rows[0]["image"].ToString();
        parent_id.SelectedValue = dt.Rows[0]["parent_id"].ToString();
    }

    protected void save_Click(object sender, EventArgs e)
    {
        string fileName = Path.GetFileName(fileUp.FileName);
        string categoryName = category_name.Text;
        string categoryDes = category_description.Text;
        string categoryImage = fileName;
        int parentID = Convert.ToInt32(parent_id.SelectedItem.Value.ToString());

        if (fileUp.HasFile)
        {
            fileUp.SaveAs(Server.MapPath("~/images/") + fileName);
        }

        if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
        {
            int result = cateBus.UpdateCategory(Convert.ToInt32(Request.QueryString["ID"]), categoryName, categoryDes, categoryImage, parentID);
            if (result == 1)
            {
                Response.Redirect("default.aspx?action=categoryList&success=Hoàn tất: Bạn đã sửa đổi Danh mục!");
            }
        }
        else
        {
            int result = cateBus.InsertCategory(categoryName, categoryDes, categoryImage, parentID);
            if (result == 1)
            {
                Response.Redirect("default.aspx?action=categoryList&success=Hoàn tất: Bạn đã thêm mới Danh mục!");
            }
        }
    }
    protected void cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx?action=categoryList");
    }
}